#!/usr/bin/env python

#Version histroy
#Ver04 : convert data to xls file format
# 

import re
import os
import sys
import glob

def MTDC2TXT(File):
    OpenFileFH=open(File,"r")
    OutputFileFH=open("".join((File,"_DCResult")),"w")
       
    TestSiteNum={}
    TestValue={}
    TestUnit={}    
    
    GlobalTestNum=0
    GlobalTestItemNum=0
    GlobalSiteNum=0
    SW=""
    
    OutputFileFH.write("\t\t\tSiteNum\t\t\tTestNum\t\t\tTestItemNum\t\t\tTestValue\t\t\tUnit\n")
    
    Lines=OpenFileFH.readlines()
    for Line in Lines:
        #find parameter test
        if re.search(r"\d+\s*\d+:\w*\s*\d+[ |-]\s*\d+",Line):
            Temp=re.split(r"\s+",Line)
            SiteNum=Temp[0]
            if len(Temp[0])>=2:
                SiteNum=Temp[0][0]
                Temp[3]=Temp[2]
                Temp[2]=Temp[1]
                Temp[1]=Temp[0][1:]
            TestNum=Temp[1][:-1]
            #Special for such as format :"1 753:A 0   650 nA    0  5   2.700 uA     300 nA    169"
            if re.search(r":\w\b",Temp[1]):
                TestNum=Temp[1][:-2]
            #Special for such as format :"1  51:C16  200185P    0  4    201000P     199000P    315"    
            elif re.search(r":\w\d+",Temp[1]):
                MyTemp=re.split(r":",Temp[1])
                TestNum=MyTemp[0]
                Temp[3]=Temp[2]
                Temp[2]=MyTemp[1][1:] 
                   
            TestItemNum=Temp[2]
            Unit=Temp[4]
            NegSign=""
            
            if re.search(r"-",Temp[2]):
                NegSign="-"
                #Special for such as format:"1  31: 19-    7 nA    0  3   1.000 uA  -1.000 uA    164"
                if Temp[2][-1]=="-":
                    TestItemNum=Temp[2][0:-1]
                #Special for such as format: "1 841: 12-89.00 uA    0  4  -31.00 uA  -162.0 uA    188"    
                else:
                    MyTemp=re.split(r"-",Temp[2])
                    TestItemNum=MyTemp[0]
                    Temp[3]=MyTemp[1]
            TempKey= "".join(("Site",SiteNum,"Test",TestNum,"Item",TestItemNum))   
            #TestValue[int(TestNum)][int(TestItemNum)]="".join((NegSign,Temp[3]))
            TestValue[TempKey]="".join((NegSign,Temp[3]))
            TestUnit[TempKey]=Unit
            TestSiteNum[TempKey]=SiteNum
            
            if GlobalTestNum < int(TestNum):
                GlobalTestNum=int(TestNum)
            
            if GlobalTestItemNum < int(TestItemNum):
                GlobalTestItemNum=int(TestItemNum)
                
            if GlobalSiteNum  < int(SiteNum):
                GlobalSiteNum=int(SiteNum)            
                
            #print TestValue[TempKey]
        # Only find the fail test value. such as "4   1: 12-3.114  V[7m*[0m   0  1  -  100 mV  -  900 mV     11"
        # \X1B is meansing  
        elif re.search("\x1B",Line):
            NegSign=""
            if re.search(r"\d+\s*\d+:\s*\d+[ |-]\s*\d+.\d+\s*\w+",Line):
                Temp=re.split("[\s+|\x1B]",Line)
                SiteNum=Temp[0]
                
                if len(Temp[0])>=2:
                    SiteNum=Temp[0][0]
                    Temp[3]=Temp[2]
                    Temp[2]=Temp[1]
                    Temp[1]=Temp[0][1:]
                TestNum=Temp[1][:-1]
                if re.search(r":\w\b",Temp[1]):
                    TestNum=Temp[1][:-2]
                
                elif re.search(r":\w\d+",Temp[1]):
                    MyTemp=re.split(r":",Temp[1])
                    TestNum=MyTemp[0]
                    Temp[3]=Temp[2]
                    Temp[2]=MyTemp[1][1:]    
                
                TestItemNum=Temp[2]
                Unit=Temp[4]            
                if re.search(r"-",Temp[2]):
                    if Temp[2][-1]=="-":
                        TestItemNum=Temp[2][0:-1]
                        NegSign="-"
                    else:
                        MyTemp=re.split("-",Temp[2])
                        Unit=Temp[3]
                        TestItemNum=MyTemp[0]
                        Temp[3]=MyTemp[1]
                        NegSign="-"
                    
                TempKey= "".join(("Site",SiteNum,"Test",TestNum,"Item",TestItemNum))   
                TestValue[TempKey]="".join((NegSign,Temp[3]))
                TestSiteNum[TempKey]=SiteNum
                TestUnit[TempKey]=Unit
                
                if GlobalTestNum <int(TestNum):
                    GlobalTestNum=int(TestNum)
                
                if GlobalTestItemNum < int(TestItemNum):
                    GlobalTestItemNum=int(TestItemNum)
                if GlobalSiteNum  < int(SiteNum):
                    GlobalSiteNum=int(SiteNum)
                
                #print TestValue[TempKey]
            elif re.search(r"\d+\s*\d+:\s*\d+\s*OVER",Line):
                Temp=re.split("\s+",Line)
                SiteNum=Temp[0]
                if len(Temp[0])>=2:
                    SiteNum=Temp[0][0]
                    Temp[3]=Temp[2]
                    Temp[2]=Temp[1]
                    Temp[1]=Temp[0][1:]
                TestNum=Temp[1][:-1]
                TestItemNum=Temp[2]
                TempKey= "".join(("Site",SiteNum,"Test",TestNum,"Item",TestItemNum))    
                TestValue[TempKey]="".join((NegSign,Temp[3]))
                TestUnit[TempKey]=""
                TestSiteNum[TempKey]=SiteNum
                
                if GlobalTestNum < int(TestNum):
                    GlobalTestNum=int(TestNum)
                
                if GlobalTestItemNum < int(TestItemNum):                
                    GlobalTestItemNum=int(TestItemNum)
                if GlobalSiteNum  < int(SiteNum):
                    GlobalSiteNum=int(SiteNum)        
                #print TestValue[TempKey]
        if re.search(r"SENSESW=",Line):
            OutputDC(OutputFileFH,TestValue,GlobalSiteNum,GlobalTestNum,GlobalTestItemNum,TestUnit)
            OutputFileFH.write("\n")
            OutputFileFH.write(Line)
            OutputFileFH.write("\n")
            TestValue={}
            TestUnit={}
            GlobalTestNum=0
            GlobalTestItemNum=0
            GlobalSiteNum=0
    if TestValue.keys():
        OutputDC(OutputFileFH,TestValue,GlobalSiteNum,GlobalTestNum,GlobalTestItemNum,TestUnit)
        TestValue={}
        TestValue={}
        TestUnit={}
        GlobalTestNum=0
        GlobalTestItemNum=0
        GlobalSiteNum=0
            
def OutputDC(OutputFileFH,TestValue,GlobalSiteNum,GlobalTestNum,GlobalTestItemNum,TestUnit):
    if TestValue.keys() :                
        for Index1 in range(GlobalSiteNum+1):
            for Index2 in range(int(GlobalTestNum)+1):
                for Index3 in range(int(GlobalTestItemNum)+1):
                    TempKey= "".join(("Site",str(Index1),"Test",str(Index2),"Item",str(Index3)))   
                    if TestValue.has_key(TempKey):
                        OutputFileFH.write("\t\t\t%4d\t\t\t%6d\t\t\t%6d\t\t\t%12s\t\t\t%6s\n"%(Index1,Index2,Index3, TestValue[TempKey],TestUnit[TempKey]))
                        #print("\t\t\t%4d\t\t\t%6d\t\t\t%6d\t\t\t%12s\t\t\t%6s\n"%(Index1,Index2,Index3, TestValue[TempKey],TestUnit[TempKey]))
                

def MTFUNC2TXT(File):
    OpenFileFH=open(File,"r")
    OutputFileFH=open("".join((File,"_FUNCResult")),"w")
    
    OutputFileFH.write("\t\t\tSiteNum\t\t\tTestNum\t\t\tTestItemNum\t\t\tTestResult\n")
    
    
    GlobalSiteNum=0
    GlobalTestNum=0
    GlobalTestResult={}
    Lines=OpenFileFH.readlines()
    for Line in Lines:
        #find functional test such as :1 804:F  0< 0> 400    0  4 NGSTOP%0000 PC%0190    267 RUNBIT=1691
        if re.search(r"\d+\s*\d+:\w\s*\d+<\s*\d+>\s*\d+\s*\d+\s*\d+\s*NGSTOP",Line):            
            Temp=re.split(r"\s+",Line)
            SiteNum=Temp[0]
            TestNum=Temp[1][:-2]
            
            
            #Special for such as format :"11900:F  0< 0>1001    0 14 NGSTOP%0000 PC%03e9    149 RUNBIT=2661"
            if len(Temp[0])>=2 :
                SiteNum=Temp[0][0]                
                TestNum=Temp[0][1:-2]
                
            if GlobalSiteNum<int(SiteNum):    
                GlobalSiteNum=int(SiteNum)
            if GlobalTestNum < int(TestNum):
                GlobalTestNum=int(TestNum)
            TempKey="".join(("Site",SiteNum,"TestNum",TestNum))
                
            GlobalTestResult[TempKey]="PASS"
            #print ("%4d%8d%8s"% (SiteNum, TestNum, "PASS"))
        elif re.search(r"\d+\s*\d+:\w\s*\d+<\s*\d+>\s*\d+\x1B\[7m\*\x1B\[0m",Line):
            
            Temp=re.split(r"\s+",Line)
            SiteNum=Temp[0]
            TestNum=Temp[1][:-2]
            
            
            #Special for such as format :"11900:F  0< 0>1001    0 14 NGSTOP%0000 PC%03e9    149 RUNBIT=2661"
            if len(Temp[0])>=2 :
                SiteNum=Temp[0][0] 
                TestNum=Temp[0][1:-2]
                
            if GlobalSiteNum<int(SiteNum):    
                GlobalSiteNum=int(SiteNum)
            if GlobalTestNum < int(TestNum):
                GlobalTestNum=int(TestNum)
            TempKey="".join(("Site",SiteNum,"TestNum",TestNum))
            GlobalTestResult[TempKey]="FAIL"
            #print ("%4d%8d%8s"% (SiteNum, TestNum, "FAIL"))
        if re.search(r"SENSESW=",Line):
            OutputFUNC(OutputFileFH,GlobalTestResult,GlobalSiteNum,GlobalTestNum)
            OutputFileFH.write("\n")
            OutputFileFH.write(Line)
            OutputFileFH.write("\n")
            GlobalSiteNum=0
            GlobalTestNum=0
            GlobalTestResult={}
    if GlobalTestResult.keys():
        OutputFUNC(OutputFileFH,GlobalTestResult,GlobalSiteNum,GlobalTestNum)
        GlobalSiteNum=0
        GlobalTestNum=0
        GlobalTestResult={}        
def OutputFUNC(OutputFileFH,GlobalTestResult,GlobalSiteNum,GlobalTestNum):
    if GlobalTestResult.keys():
        for Index1 in range(GlobalSiteNum+1):
            for Index2 in range(GlobalTestNum+1):
                TempKey="".join(("Site",str(Index1),"TestNum",str(Index2)))   
                if GlobalTestResult.has_key(TempKey):
                    OutputFileFH.write("%8d%8s%8s\n"% (Index1, Index2, GlobalTestResult[TempKey]))                       

def main(PrintDC):
    
    Files=glob.glob("*")
    
    if PrintDC:
        for File in Files:
            if os.path.isfile(File) and re.search(r"^MT-\d+\-\d+$",File):
                MTDC2TXT(File)
    else:
        for File in Files:
            if os.path.isfile(File) and re.search(r"^MT-\d+\-\d+$",File):
                MTFUNC2TXT(File)
    #MT2TXT("d4-070424-1.txt")
                
if __name__ == '__main__':
    
    if len(sys.argv)>1:
        PrintDC =False
    else:
        PrintDC=True
        
        print "##############################"
        print "##Default convert DC value  ##"
        print "##if you want convert FUNC  ##"
        print "## test result plsease fol- ##"
        print "##lowing any parameter!     ##"
        print "##############################"
        
        
    main(PrintDC)